import {
  reqGetDeviceDetail,
  reqPrint,
  reqDeleteDeviceById
} from '@/api/check_detail.js'

export default {
  name: 'hand_detail_common',
  data () {
    return {
      detail: null,
      isDisabled: false,
      isLoading: false
    }
  },
  mounted () {},
  methods: {

    // 删除
    deleteDeviceById (deviceId, idArr) {
      this.$confirm('确认要删除此设备嘛?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          if (Array.isArray(idArr) && idArr.length > 0) {
            for (let i = 0; i < idArr.length; i++) {
              const deviceId = idArr[i]
              const res = await reqDeleteDeviceById(deviceId)
              if (res.data.code == 0) {
                this.$message({
                  message: '删除成功',
                  type: 'success'
                })
                this.detail = null

                this.checkStatisticsBus.$emit('getTypeAndNumByPo')
              } else {
                this.$message.error(res.data.msg)
              }
            }
          } else {
            const res = await reqDeleteDeviceById(deviceId)
            if (res.data.code == 0) {
              this.$message({
                message: '删除成功',
                type: 'success'
              })
              this.detail = null
              this.checkStatisticsBus.$emit('getTypeAndNumByPo')
            } else {
              this.$message.error(res.data.msg)
            }
          }
        })
        .catch((err) => {
          this.$message({
            message: '取消删除',
            type: 'info'
          })
        })
    },
    // 打印
    async print (deviceId, userId, idArr) {
      this.isDisabled = true
      this.isLoading = true
      if (Array.isArray(idArr) && idArr.length > 0) {
        for (let i = 0; i < idArr.length; i++) {
          const deviceId = idArr[i]
          const res = await reqPrint(deviceId, userId)
          if (res.data.code == 0) {
            this.$message({
              message: '打印成功',
              type: 'success'
            })
          } else {
            console.log(res.data.msg)
            this.$message.error(res.data.msg)
          }
        }
      } else {
        const res = await reqPrint(deviceId, userId)
        if (res.data.code == 0) {
          this.$message({
            message: '打印成功',
            type: 'success'
          })
        } else {
          this.$message.error(res.data.msg)
        }
      }

      this.operating_delay()
    },
    // 获取设备详情
    async getDeviceDetail (id) {
      const res = await reqGetDeviceDetail(id)
      if (res.data.code == 0) {
        this.detail = res.data.data
      } else {
        this.$message.error(res.data.msg)
      }
    },
    // 操作延时
    operating_delay () {
      setTimeout(() => {
        this.isDisabled = false
        this.isLoading = false
      }, 3000)
    }

  }
}
